<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnConn").click(function() {
                var ip = $("#ip").val();
                var port = $("#port").val();
                var catchMsg = $("#catchMsg");

                //判断浏览器是否支持WebSocket
                var supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window;
                if (supportsWebSockets) {
                    //建立WebSocket连接（ip地址换成自己主机ip）
                    var url = "ws://" + ip + ":" + port + "/";
                    var ws = new WebSocket(url);
                    ws.onopen = function () {
                        //当WebSocket创建成功时，触发onopen事件
                        console.log("websocket连接成功");
                        //ws.send("hello"); //将消息发送到服务端
                        ws.send(
                            JSON.stringify({
                                symbols: "['00700.hk]",
                                command: 0,
                                version: '1.0.0',
                                token: 'szfiu',
                                heartBeat: 'heartBeat'
                            })
                        );
                    }
                    ws.onmessage = function (e) {
                        //当客户端收到服务端发来的消息时，触发onmessage事件，参数e.data包含server传递过来的数据
                        console.log("收到数据");
                        console.log(e.data);
                        catchMsg.append(e.data);
                    }
                    ws.onclose = function (e) {
                        //当客户端收到服务端发送的关闭连接请求时，触发onclose事件
                        console.log("websocket已断开");
                    }
                    ws.onerror = function (e) {
                        //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
                        console.log("websocket发生错误" + e);
                    }
                } else {
                    layer.alert("您的浏览器不支持 WebSocket!");
                }
            });

        });
    </script>
</head>
<body>
<div>
    <input type="text" id="ip" value="localhost"/>
    <input type="text" id="port" value="8988"/>
    <input type="button" value="connect" id="btnConn"/>
</div>
<div>
    <textarea id="catchMsg"></textarea>
</div>
</body>
</html>